package yanyan.com.tochar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.ToastUtil;


public class FunsVideoResolveActivity extends Activity {
    private ProgressDialog progressDialog;

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_video2);
        webView=findViewById(R.id.video_web);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.supportMultipleWindows();  //多窗口
        settings.setAllowFileAccess(true);  //设置可以访问文件
        settings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

   if(CoreUtil.appInfo==null || CoreUtil.appInfo.getOtherUrl().length()<5){
       ToastUtil.showShotToast(this,"资源加载失败");
   }else {
       webView.loadUrl(CoreUtil.appInfo.getOtherUrl());
   }


        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //这里一定要是url  不能是网页
                view.loadUrl(url);
                return true;
            }

            //证书的设置  支持所有的证书
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

        });

        //加载
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100 && progressDialog!=null && progressDialog.isShowing()){
                    progressDialog.dismiss();
                }else {
                    if(progressDialog==null) {
                        progressDialog = new ProgressDialog(FunsVideoResolveActivity.this);
                        progressDialog.setTitle("正在载入");
                        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        progressDialog.setProgress(newProgress);
                        progressDialog.show();
                    }
                    else {
                        progressDialog.setProgress(newProgress);
                    }
                }

            }
        });


    }



    //防止退出的时候  抛Zoom异常
    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }


    /**
     * 返回
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK  && !webView.getUrl().equals(CoreUtil.appInfo.getOtherUrl())) {
            if(webView.canGoBack()) {
                webView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
